package com.lvchangqi.util;

import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

/**
 * 本类对Jmail实现调用封装
 * 可内嵌html标签
 * @Filed subject jmail 标题
 * @Filed content jmail 内容
 * @Filed toAddress jmail 发送对象
 * @author 吕长奇
 *
 */

public class MailBuilder{
    private final String docx = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";

    private String subject;
    private String content;
    private String toAddress;

    public void setToAddress(String toAddress) {
        this.toAddress = toAddress;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public MailBuilder() {
        super();
    }
    public MailBuilder(String subject, String content, String toAddress){
        super();
        this.subject = subject;
        this.content = content;
        this.toAddress = toAddress;
    }

    public void build(InputStream in) throws Exception {
        Properties pro = new Properties();
        //协议服务商
        pro.setProperty("mail.smtp", "smtp.sina.com.cn");
        //邮件协议
        pro.setProperty("mail.transport.protocol", "smtp");
        //用户验证
        pro.setProperty("mail.smtp.auth", "true");
        //创建session
        Session session = Session.getInstance(pro);
        //打开debug模式
//        session.setDebug(true);
        //得到transport对象
        Transport transport = session.getTransport();
        //连接服务商
        transport.connect("smtp.qq.com","tiantang201@qq.com", "daxieA0201");
        //创建邮件
        Message message = this.createMail(session, in);
        //发送邮件
        transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
        transport.close();
        in.close();
    }

    /**
     * 类内部调用
     * @param session
     * @return
     * @throws Exception
     */
    private MimeMessage createMail(Session session, InputStream in) throws Exception{
        //邮件对象
        MimeMessage mime = new MimeMessage(session);
        //发件人
        mime.setFrom(new InternetAddress("tiantang201@qq.com"));
        //收件人
        mime.setRecipient(Message.RecipientType.TO, new InternetAddress(this.toAddress));
        //标题
        mime.setSubject(this.subject);
        //附件
        MimeBodyPart mimeBodyPart0 = this.attrMineBodyPart(in, "湖北师范大学查课查寝请假审批表.docx");
        //内容
        MimeBodyPart mimeBodyPart1 = new MimeBodyPart();
        mimeBodyPart1.setContent(this.content, "text/html;charset=UTF-8");
        //邮件实体
        MimeMultipart mimeMultipart = new MimeMultipart(mimeBodyPart0, mimeBodyPart1);
        //发送时间
        mime.setSentDate(new Date());
        //邮件内容
        mime.setContent(mimeMultipart);
        mime.saveChanges();

        return mime;
    }

    /**
     * 插入附件
     * @param in
     * @return
     */
    private MimeBodyPart attrMineBodyPart(InputStream in, String fileName) throws
            MessagingException, IOException {
        MimeBodyPart mimeBodyPart = new MimeBodyPart();


        mimeBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(in, this.docx)));
        mimeBodyPart.setFileName(MimeUtility.encodeWord(fileName));

        return mimeBodyPart;
    }
}